import request from 'wechat-http'
/**
 * 配置接口基础路径
 */
const baseURL = 'https://live-api.itheima.net'
request.baseURL = baseURL

request.intercept.request = (params) => {
  const { token } = getApp();
  if (token) {
    params.header = {
      Authorization: token
    }
  }
  return params
}
request.intercept.response = ({ data, statusCode }) => {
  if (data.code != 1e4) {
    wx.utils.toast(data.message, 'error')
    if (statusCode === 401) {
      const pages = getCurrentPages()
      const currPage = pages[pages.length - 1]
      wx.redirectTo({
        url: `/pages/login/index?redirectUrl=/${currPage.route}`,
      });
    }
    return Promise.reject(data)
  }
  return data
}
/**
 * 挂载方法到全局
 */
wx.http = request
wx.baseURL = baseURL